
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// This file was initially generated by gen_to_start.go (add link), as a start
// of the Traffic Ops golang data model

package api

import (
	"encoding/json"
	_ "github.com/apache/incubator-trafficcontrol/traffic_ops/experimental/server/output_format" // needed for swagger
	"github.com/jmoiron/sqlx"
	null "gopkg.in/guregu/null.v3"
	"log"
	"time"
)

type PhysLocations struct {
	Name      string             `db:"name" json:"name"`
	ShortName string             `db:"short_name" json:"shortName"`
	Address   string             `db:"address" json:"address"`
	City      string             `db:"city" json:"city"`
	State     string             `db:"state" json:"state"`
	Zip       string             `db:"zip" json:"zip"`
	Poc       null.String        `db:"poc" json:"poc"`
	Phone     null.String        `db:"phone" json:"phone"`
	Email     null.String        `db:"email" json:"email"`
	Comments  null.String        `db:"comments" json:"comments"`
	Region    string             `db:"region" json:"region"`
	CreatedAt time.Time          `db:"created_at" json:"createdAt"`
	Links     PhysLocationsLinks `json:"_links" db:-`
}

type PhysLocationsLinks struct {
	Self string `db:"self" json:"_self"`
}

// @Title getPhysLocationsById
// @Description retrieves the phys_locations information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    PhysLocations
// @Resource /api/2.0
// @Router /api/2.0/phys_locations/{id} [get]
func getPhysLocation(name string, db *sqlx.DB) (interface{}, error) {
	ret := []PhysLocations{}
	arg := PhysLocations{}
	arg.Name = name
	queryStr := "select *, concat('" + API_PATH + "phys_locations/', name) as self"
	queryStr += " from phys_locations WHERE name=:name"
	nstmt, err := db.PrepareNamed(queryStr)
	err = nstmt.Select(&ret, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	nstmt.Close()
	return ret, nil
}

// @Title getPhysLocationss
// @Description retrieves the phys_locations
// @Accept  application/json
// @Success 200 {array}    PhysLocations
// @Resource /api/2.0
// @Router /api/2.0/phys_locations [get]
func getPhysLocations(db *sqlx.DB) (interface{}, error) {
	ret := []PhysLocations{}
	queryStr := "select *, concat('" + API_PATH + "phys_locations/', name) as self"
	queryStr += " from phys_locations"
	err := db.Select(&ret, queryStr)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return ret, nil
}

// @Title postPhysLocations
// @Description enter a new phys_locations
// @Accept  application/json
// @Param                 Body body     PhysLocations   true "PhysLocations object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/phys_locations [post]
func postPhysLocation(payload []byte, db *sqlx.DB) (interface{}, error) {
	var v PhysLocations
	err := json.Unmarshal(payload, &v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "INSERT INTO phys_locations("
	sqlString += "name"
	sqlString += ",short_name"
	sqlString += ",address"
	sqlString += ",city"
	sqlString += ",state"
	sqlString += ",zip"
	sqlString += ",poc"
	sqlString += ",phone"
	sqlString += ",email"
	sqlString += ",comments"
	sqlString += ",region"
	sqlString += ",created_at"
	sqlString += ") VALUES ("
	sqlString += ":name"
	sqlString += ",:short_name"
	sqlString += ",:address"
	sqlString += ",:city"
	sqlString += ",:state"
	sqlString += ",:zip"
	sqlString += ",:poc"
	sqlString += ",:phone"
	sqlString += ",:email"
	sqlString += ",:comments"
	sqlString += ",:region"
	sqlString += ",:created_at"
	sqlString += ")"
	result, err := db.NamedExec(sqlString, v)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title putPhysLocations
// @Description modify an existing phys_locationsentry
// @Accept  application/json
// @Param   id              path    int     true        "The row id"
// @Param                 Body body     PhysLocations   true "PhysLocations object that should be added to the table"
// @Success 200 {object}    output_format.ApiWrapper
// @Resource /api/2.0
// @Router /api/2.0/phys_locations/{id}  [put]
func putPhysLocation(name string, payload []byte, db *sqlx.DB) (interface{}, error) {
	var arg PhysLocations
	err := json.Unmarshal(payload, &arg)
	arg.Name = name
	if err != nil {
		log.Println(err)
		return nil, err
	}
	sqlString := "UPDATE phys_locations SET "
	sqlString += "name = :name"
	sqlString += ",short_name = :short_name"
	sqlString += ",address = :address"
	sqlString += ",city = :city"
	sqlString += ",state = :state"
	sqlString += ",zip = :zip"
	sqlString += ",poc = :poc"
	sqlString += ",phone = :phone"
	sqlString += ",email = :email"
	sqlString += ",comments = :comments"
	sqlString += ",region = :region"
	sqlString += ",created_at = :created_at"
	sqlString += " WHERE name=:name"
	result, err := db.NamedExec(sqlString, arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}

// @Title delPhysLocationsById
// @Description deletes phys_locations information for a certain id
// @Accept  application/json
// @Param   id              path    int     false        "The row id"
// @Success 200 {array}    PhysLocations
// @Resource /api/2.0
// @Router /api/2.0/phys_locations/{id} [delete]
func delPhysLocation(name string, db *sqlx.DB) (interface{}, error) {
	arg := PhysLocations{}
	arg.Name = name
	result, err := db.NamedExec("DELETE FROM phys_locations WHERE name=:name", arg)
	if err != nil {
		log.Println(err)
		return nil, err
	}
	return result, err
}
